package com.fenghongzhang.day004;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void btn(View view) {

        //1 创建被观察者.
        Observable observable = Observable.create(new ObservableOnSubscribe() {
            @Override
            public void subscribe(ObservableEmitter emitter) throws Exception {
                emitter.onNext("1");
//                emitter.onError(new RuntimeException("大丰收的"));
                emitter.onNext("2");
                emitter.onComplete();
            }
        });

        //用just创建
//        Observable observable1 = Observable.just("1","2");
//        String[]  s = new String[]{"1","2"};
//        Observable observable1 = Observable.fromArray(s);


        //2 观察者
        Observer observer = new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                Log.i(TAG, "onSubscribe: ");
            }

            @Override
            public void onNext(String s) {
                Log.i(TAG, "onNext: "+s);
            }

            @Override
            public void onError(Throwable e) {
                Log.i(TAG, "onError: ");
            }

            @Override
            public void onComplete() {
                Log.i(TAG, "onComplete: ");
            }
        };

        //绑定
        observable.subscribe(observer);

    }

    public void btn1(View view) {

        Observable.create(new ObservableOnSubscribe() {
            @Override
            public void subscribe(ObservableEmitter emitter) throws Exception {
                emitter.onNext("1");
            }
        }).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(String s) {
                Log.i(TAG, "onNext: "+s);
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    public void btn2(View view) {
        //合并操作符
        Observable.merge(Observable.just("1"),Observable.just("2"))
                .subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(String s) {
                Log.i(TAG, "onNext: "+s);
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    public void btn3(View view) {

        Observable.create(new ObservableOnSubscribe() {
            @Override
            public void subscribe(ObservableEmitter emitter) throws Exception {
                emitter.onNext("1");
            }
        }).observeOn(AndroidSchedulers.mainThread()) //主线性
            .subscribeOn(Schedulers.io())//工作线性
            .subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
            }

            @Override
            public void onNext(String s) {
                Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
//                Log.i(TAG, "onNext: "+s);
            }

            @Override
            public void onError(Throwable e) {
            }
            @Override
            public void onComplete() {
            }
        });
    }

    public void btn4(View view) {
        //1导包
        //2.创建Retrofit
        //http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=20&page=1
        Retrofit build = new Retrofit.Builder()
                .baseUrl("http://www.qubaobei.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        //restful 调用
        Api api = build.create(Api.class);

        Call<Food> call = api.getCall();
        call.enqueue(new Callback<Food>() {
            @Override
            public void onResponse(Call<Food> call, Response<Food> response) {
                Food body = response.body();
                Log.i(TAG, "onResponse: "+body.getData().size());
            }

            @Override
            public void onFailure(Call<Food> call, Throwable t) {
                Log.i(TAG, "onFailure: ");
            }
        });

    }

    public void btn5(View view) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://www.qubaobei.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        Api api = retrofit.create(Api.class);
        //返回正好就是被观察者
//        Observable<Food> foodCall = api.getFoodCall1("2");
//        Observable<Food> foodCall = api.getFoodCall3("1");
        Observable<Food> foodCall = api.getFoodCall4("ios");
        foodCall.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Food>() {
            @Override
            public void onSubscribe(Disposable d) {
                Log.i(TAG, "onSubscribe: ");
            }
            @Override
            public void onNext(Food food) {
                Log.i(TAG, "onNext: "+food.getData().size());
                Toast.makeText(MainActivity.this, food.getData().size()+"", Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onError(Throwable e) {
                Log.i(TAG, "onError: ");
            }
            @Override
            public void onComplete() {
            }
        });

    }
}
